package com.cqyc.shop.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqyc.shop.comm.StringUtil;
import com.cqyc.shop.comm.commException;
import com.cqyc.shop.domain.Shop;
import com.cqyc.shop.services.ShopServices;


@WebServlet(urlPatterns="/shopadd.do")
@MultipartConfig(
		 maxFileSize = 5*1024*1024  
		 )
public class ShopAddServlet extends HttpServlet{
	private static final long serialVersionUID = -1474676497198691375L;
	private Logger logger = LoggerFactory.getLogger(getClass());
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private ShopServices shopServices = new ShopServices();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("商品存储开始");
		try {
			//1:从前台获取参数
			Part part = request.getPart("picture");
			if(part == null) {
			throw new commException("必须要选择文件哟，宝贝");
			}
			String path="D:/mywork";//文件存放路径
			//保存在服务器上的文件名称,用来保证服务器上的文件名称唯一
			String filename = StringUtil.generateUUID()+"-"+part.getSubmittedFileName();
			part.write(path + "/" + filename);//保存文件
			logger.info("保存文件路径:{}/{}",path,filename);
			
			String id = request.getParameter("id");
			String shopname = request.getParameter("shopname");
			String content = request.getParameter("content");
			String price = request.getParameter("price");
			String producedate = request.getParameter("producedate");
			String produce = request.getParameter("produce");
			String shoptotal =request.getParameter("shoptotal");
			String picture = "/"+filename;
			logger.debug("{}",id);
			Shop shop = new Shop();
			shop.setShopname(shopname);
			shop.setShopinfo(content);
			shop.setProduce(produce);
			shop.setShopimg(picture);
			shop.setShoptotal(Integer.valueOf(shoptotal));
			//2:数据转换
			if(id==null || "".equals(id)) {
				shop.setId(null);
			}else{
				shop.setId(Integer.valueOf(id));
			}
			shop.setProducedate(df.parse(producedate));
			shop.setPrice(new BigDecimal(price));
			
			//3:数据校验
			if(shopname == null || content == null || produce==null ) {
				throw new commException("所有字段不能为空");
			}
			//4:业务处理
			shopServices.save(shop);
			//5:跳转
			response.sendRedirect(request.getContextPath()+"/index.do");
			logger.debug("成功保存");
		} catch (Exception e) {
			logger.error("{}",e.getMessage());
			request.setAttribute("error",e.getMessage());
			request.getRequestDispatcher("/admin_insert.jsp").forward(request, response);
		}
	}
	
}
